package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.wbao.dianniu.R;
import com.wbao.dianniu.utils.Utils;

public class NoScrollGridAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<String> mList;
    private HashMap<Integer, View> viewMap;
    private boolean isShow = true;
    private String pic_zoom_rule;
    private int size;

    public NoScrollGridAdapter(Context context, ArrayList<String> datalist, boolean isShow) {
        mContext = context;
        mList = datalist;
        viewMap = new HashMap<>();
        this.isShow = isShow;
        size = Utils.dip2px(mContext, 80);
        pic_zoom_rule = "@"+size+"w_"+size+"h_1e";
    }

    public void setData(ArrayList<String> dataList) {
        mList = dataList;
    }

    @Override
    public int getCount() {
        if (null == mList)
            return 0;
        else
            return mList.size();
    }

    @Override
    public Object getItem(int position) {
        if (position < mList.size()) {
            return mList.get(position);
        } else {
            return 0;
        }
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if (!viewMap.containsKey(position) || viewMap.get(position) == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_gridview, null);
            holder.imageview = convertView.findViewById(R.id.iv_image);
            convertView.setTag(holder);
            viewMap.put(position, convertView);
        } else {
            convertView = viewMap.get(position);
            holder = (Holder) convertView.getTag();
        }

        if (isShow) {
//            Log.e("PIC","pic:" + mList.get(position) + pic_zoom_rule);
            Glide.with(mContext).load(mList.get(position) + pic_zoom_rule).centerCrop().crossFade()
                    /*.thumbnail(0.1f)*/.into(holder.imageview);
        } else {
            Glide.with(mContext).load(R.color.tabhost_bg).centerCrop().crossFade()
                    .thumbnail(0.1f).into(holder.imageview);
        }

        return convertView;
    }

    private class Holder {
        ImageView imageview;
    }

}
